﻿Imports System.Xml
Imports System.IO
Imports System.Reflection

Public Class settings
#Region "Variables"
    Private Shared malarmtime As TimeSpan
    Private Shared malarmsound As String
    Private Shared mHours As Integer
    Private Shared mMinutes As Integer
    Private Shared mSeconds As Integer
    Private Shared mloopChecked As Boolean
    Private Shared mSnoozeMinutes As Integer
#End Region
#Region "Functions"
    Public Shared Sub refresh()
        Dim xdoc As XDocument

        malarmsound = ""
        malarmtime = New TimeSpan(0)

        Try
            xdoc = XDocument.Load(GetAppPath() & "\Hourglass.settings")
            For Each Element In xdoc.Root.Elements
                Select Case Element.Name.ToString
                    Case "Settings"
                        For Each Setting In Element.Elements
                            Select Case Setting.Name.ToString
                                Case "AlarmSound"
                                    Try
                                        malarmsound = Setting.Attribute("File").Value
                                        'mrepeat = Setting.Attribute("Repeat").Value 
                                    Catch
                                    End Try
                                Case "AlarmTime-Hours"
                                    Try
                                        mHours = Setting.Attribute("Time").Value
                                    Catch
                                    End Try
                                Case "AlarmTime-Minutes"
                                    Try
                                        mMinutes = Setting.Attribute("Time").Value
                                    Catch
                                    End Try
                                Case "AlarmTime-Seconds"
                                    Try
                                        mSeconds = Setting.Attribute("Time").Value
                                    Catch
                                    End Try
                                Case "SnoozeTime-Minutes"
                                    Try
                                        mSnoozeMinutes = Setting.Attribute("Time").Value
                                    Catch
                                    End Try
                                Case "LoopCheck"
                                    Try
                                        mloopChecked = Setting.Attribute("Checked").Value
                                    Catch
                                    End Try
                            End Select
                        Next
                End Select
            Next
        Catch
        End Try
    End Sub
    'Private Shared Function StringToTimeSpan(ByVal Time As String) As TimeSpan
    '    Dim Hours As Integer = 0
    '    Dim Minutes As Integer = 0
    '    Dim Seconds As Integer = 0
    '    Dim Parts() As String

    '    On Error Resume Next
    '    Parts = Split(Time, ":")
    '    Hours = Val(Parts(0))
    '    Minutes = Val(Parts(1))
    '    Seconds = Val(Parts(2))

    '    Return New TimeSpan(Hours, Minutes, Seconds)
    'End Function
    Private Shared Function StringToXML(ByVal Text As String) As String
        Text = Text.Replace("""", "&quot;")
        Text = Text.Replace("&", "&amp;")
        Text = Text.Replace("'", "&apos;")
        Text = Text.Replace("<", "&gt;")
        Text = Text.Replace(">", "&lt;")
        Return Text
    End Function
    Public Shared Sub Sync()
        Dim XML As String
        Dim Stream As FileStream
        Dim Bytes() As Byte

        On Error Resume Next
        XML = "<Hourglass>" & vbCrLf & _
                "   <Settings>" & vbCrLf & _
                "      <AlarmSound File=""" & StringToXML(malarmsound) & """/>" & vbCrLf & _
                "      <AlarmTime-Hours Time=""" & Hours & """/>" & vbCrLf & _
                "      <AlarmTime-Minutes Time=""" & Minutes & """/>" & vbCrLf & _
                "      <AlarmTime-Seconds Time=""" & Seconds & """/>" & vbCrLf & _
                "      <SnoozeTime-Minutes Time=""" & SnoozeMinutes & """/>" & vbCrLf & _
                "      <LoopCheck Checked=""" & LoopChecked & """/>" & vbCrLf & _
                "   </Settings>" & vbCrLf & _
                "</Hourglass>" & vbCrLf

        Stream = File.OpenWrite(GetAppPath() & "\Hourglass.settings")
        Bytes = System.Text.ASCIIEncoding.ASCII.GetBytes(XML)
        Stream.Write(Bytes, 0, Bytes.Length)
        Stream.Close()
    End Sub
    Private Shared Function GetAppPath() As String

        Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()

        Return System.IO.Path.GetDirectoryName(asm.GetName().CodeBase)

    End Function
    'Public Sub sync()
    '    Dim xdoc As XDocument

    '    Try
    '        xdoc = XDocument.Load(GetAppPath() & "\Hourglass.settings")
    '        For Each Element In xdoc.Root.Elements
    '            Select Case Element.Name.ToString
    '                Case "Settings"
    '                    For Each Setting In Element.Elements
    '                        Select Case Setting.Name.ToString
    '                            Case "AlarmSound"
    '                                Try
    '                                    Setting.Attribute("File").Value = malarmsound
    '                                    'mrepeat = Setting.Attribute("Repeat").Value 
    '                                Catch
    '                                End Try
    '                            Case "AlarmTime"
    '                                Try
    '                                    Setting.Attribute("Time").Value = malarmtime.Hours & ":" & malarmtime.Minutes & ":" & malarmtime.Seconds
    '                                Catch
    '                                End Try
    '                        End Select
    '                    Next
    '            End Select
    '        Next

    '        xdoc.Save(GetAppPath() & "\Hourglass.settings")
    '    Catch
    '    End Try
    'End Sub
#End Region
#Region "Properties"
    Public Shared Property Alarmtime() As TimeSpan
        Get
            Return malarmtime
        End Get
        Set(ByVal value As TimeSpan)
            malarmtime = value
        End Set
    End Property
    Public Shared Property LoopChecked() As Boolean
        Get
            Return mloopChecked
        End Get
        Set(ByVal value As Boolean)
            mloopChecked = value
        End Set
    End Property
    Public Shared Property alarmsound() As String
        Get
            Return malarmsound
        End Get
        Set(ByVal value As String)
            malarmsound = value
        End Set
    End Property
    Public Shared Property Hours() As Integer
        Get
            Return mHours
        End Get
        Set(ByVal value As Integer)
            mHours = value
        End Set
    End Property
    Public Shared Property Minutes() As Integer
        Get
            Return mMinutes
        End Get
        Set(ByVal value As Integer)
            mMinutes = value
        End Set
    End Property
    Public Shared Property Seconds() As Integer
        Get
            Return mSeconds
        End Get
        Set(ByVal value As Integer)
            mSeconds = value
        End Set
    End Property
    Public Shared Property SnoozeMinutes() As Integer
        Get
            Return mSnoozeMinutes
        End Get
        Set(ByVal value As Integer)
            mSnoozeMinutes = value
        End Set
    End Property
#End Region
End Class
